package org.llc.flink.study.example

import scala.collection.mutable


object List13 {
  def main(args: Array[String]): Unit = {
    val lines = List("atguigu han hello", "atguigu han aaa aaa aaa ccc ddd uuu")
    val res = lines.foldLeft(mutable.Map[String, Int]())(wordCount)
    // _._1 根据key排序，_._2根据value排序，默认都是升序
    //println(res.toSeq.sortBy(_._2))
    println(res.toSeq.sortWith(_._2 > _._2))
  }

  def wordCount(map: mutable.Map[String, Int], str: String): mutable.Map[String, Int] = {
    val strs = str.split(" ")
    for (word <- strs) {
      if (map.contains(word)) {
        val count = map.get(word).get
        map += (word -> (count + 1))
      } else {
        map += (word -> 1)
      }
    }
    map
  }

}
